//导入axios
import axios from "axios";
import { Toast } from "vant";

//创建axios实例
const request = axios.create({
  baseURL: "http://interview-api-t.itheima.net/h5/",
});

request.interceptors.request.use(
  function (config) {
    if (localStorage.getItem("token")) {
      config.headers.Authorization = `Bearer ${localStorage.getItem("token")}`;
    }
    return config;
  },
  function (error) {
    return Promise.reject(error);
  }
);

request.interceptors.response.use(
  function (response) {
    return response.data;
  },
  function (error) {
    if (error.response) Toast.fail(error.response.data.message);
    return Promise.reject(error);
  }
);
//导出axios实例
export default request;
